package com.meowcook.interceptor;

import com.meowcook.config.AppConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    /* @Value("${meowcook.login.url}")
    private String defaultUrl; */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取session信息
        HttpSession session = request.getSession(false);
        // 根据session是否为空判断用户是否登录
        if (session == null || session.getAttribute(AppConfig.USER_SESSION_KEY) == null) {
            log.info("用户未登录");
            return false;
        }
        return true;
    }
}
